package com.hmall.service;

import com.hdmall.business.common.common.ResponseCode;
import com.hdmall.business.common.common.ServerResponse;
import com.hdmall.business.common.vo.AddressResultVo;
import com.hmall.mapper.BiUserActionHistoryMapper;
import com.hmall.model.BiUserActionHistory;
import com.hmall.model.SysUser;
import com.hdmall.business.common.util.IpUtil;
import com.hdmall.business.common.util.PhoneUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户行为服务层
 */
@Service
public class BiUserActionHistoryService {
    private final BiUserActionHistoryMapper biUserActionHistoryMapper;

    @Autowired
    public BiUserActionHistoryService(BiUserActionHistoryMapper biUserActionHistoryMapper) {
        this.biUserActionHistoryMapper = biUserActionHistoryMapper;
    }

    public ServerResponse insert(HttpServletRequest request, SysUser user, int userRole , String action, String actionUrl, String content){
        BiUserActionHistory actionHistory=new BiUserActionHistory();
        actionHistory.setAction(action);
        actionHistory.setActionUrl(actionUrl);
        actionHistory.setContent(content);
        actionHistory.setUserRole(userRole);
        String agent=request.getHeader("user-agent");
        if(agent.toLowerCase().contains("mac")||agent.toLowerCase().contains("ios")){
            actionHistory.setDeviceName("苹果:"+agent.substring(0,agent.indexOf(")")+1));
            actionHistory.setDeviceType(2);
            //deviceVo.setDeviceName("苹果");
        }else if(agent.toLowerCase().contains("android")){
            actionHistory.setDeviceName("安卓:"+agent.substring(0,agent.indexOf(")")));
            actionHistory.setDeviceType(1);
        }else{
            actionHistory.setDeviceType(3);
            actionHistory.setDeviceName("PC:"+agent.substring(0,agent.indexOf(")")));
        }
        System.out.println(agent);
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        actionHistory.setIp(ip);
        AddressResultVo addressResult=IpUtil.getBaiduPosition(ip);
        if (addressResult.getStatus().equals("0")){//状态0 正常
           String province= addressResult.getContent().getAddress_detail().getProvince();
           String city= addressResult.getContent().getAddress_detail().getCity();
           if(province!=null&&!"".equals(province)){
               actionHistory.setProvince(province);
           }
           if(city!=null&&!"".equals(city)){
               actionHistory.setCity(city);
           }
        }
        if(user!=null){
            actionHistory.setUserId(user.getUserId());
            //看是买方还是卖方
            if(user.getPhone()!=null&&!"".equals(user.getPhone())){
                actionHistory.setPhone(user.getPhone());
                String getZoneResult=PhoneUtil.calcMobileCity(user.getPhone());
                if(!"".equals(getZoneResult)){
                   String carrier= getZoneResult.substring(getZoneResult.indexOf("carrier")+"carrier".length()+1,getZoneResult.length()-1);
                   actionHistory.setPhoneProvince(carrier);
                }
            }
        }

        try {
            biUserActionHistoryMapper.insertUserActionHis(actionHistory);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.USER_BI_ACT_HIS_FOUND.getCode(),ResponseCode.USER_BI_ACT_HIS_FOUND.getDesc());
        }

        return ServerResponse.createBySuccess("用户历史插入成功");

    }

    public static void main(String[] args) {
       /* String __GetZoneResult_="__GetZoneResult_ = {    mts:'1835891',    province:'浙江',    catName:'中国移动',    telString:'18358912521',\tareaVid:'30510',\tispVid:'3236139',\tcarrier:'浙江移动'}";
        __GetZoneResult_=__GetZoneResult_.substring(__GetZoneResult_.indexOf("carrier")+"carrier".length()+1,__GetZoneResult_.length()-1);
        System.out.println(__GetZoneResult_);*/
    }

}
